package com.zishi.venuesbooking.ac;

import android.widget.ImageView;
import android.widget.SeekBar;

import androidx.viewpager2.widget.ViewPager2;

import com.google.android.exoplayer2.ExoPlayer;
import com.google.android.exoplayer2.MediaItem;
import com.google.android.exoplayer2.source.MediaSource;
import com.google.android.exoplayer2.source.ProgressiveMediaSource;
import com.google.android.exoplayer2.ui.StyledPlayerView;
import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory;
import com.zishi.venuesbooking.BaseActivity;
import com.zishi.venuesbooking.R;
import com.zishi.venuesbooking.adapter.VideoAdapter;

import org.json.JSONException;

import java.io.IOException;
import java.util.ArrayList;

public class VideoActivity extends BaseActivity {

    private ViewPager2 viewPager2;
    private ArrayList<String> videoUrls;  // 存储视频地址
    private ExoPlayer exoPlayer;
    private ImageView playPauseButton;
    private ImageView fullScreenButton;
    private ImageView likeButton;
    private ImageView shareButton;
    private ImageView favoriteButton;
    private ImageView commentButton;
    private SeekBar progressBar;
    private StyledPlayerView playerView;
    private boolean isPlaying = false;
    private boolean isLiked = false;
    private boolean isFavorited = false;

    @Override
    protected int initLayout() {
        return R.layout.activity_video;
    }

    @Override
    protected void initView() {
        // 初始化视图
        viewPager2 = findViewById(R.id.viewPager2);
        playPauseButton = findViewById(R.id.playPauseButton);
        fullScreenButton = findViewById(R.id.fullScreenButton);
        likeButton = findViewById(R.id.likeButton);
        shareButton = findViewById(R.id.shareButton);
        favoriteButton = findViewById(R.id.favoriteButton);
        commentButton = findViewById(R.id.commentButton);
        progressBar = findViewById(R.id.progressBar);
        playerView = findViewById(R.id.playerView);
    }

    @Override
    protected void initData() throws JSONException, IOException {
        // 初始化视频 URL
        videoUrls = new ArrayList<>();
        videoUrls.add("asset:///videos/sample_video_1.mp4");
        videoUrls.add("asset:///videos/sample_video_2.mp4");
        videoUrls.add("asset:///videos/sample_video_3.mp4");

        // 初始化 ExoPlayer
        exoPlayer = new ExoPlayer.Builder(this).build();
        playerView.setPlayer(exoPlayer);

        // 配置 ViewPager2
        VideoAdapter adapter = new VideoAdapter(videoUrls, new VideoAdapter.OnVideoClickListener() {
            @Override
            public void onClick(String videoUrl) {
                // 播放视频
                playVideo(videoUrl);
            }
        });
        viewPager2.setAdapter(adapter);

        // 播放/暂停按钮点击事件
        playPauseButton.setOnClickListener(v -> togglePlayPause());

        // 全屏按钮点击事件
        fullScreenButton.setOnClickListener(v -> toggleFullScreen());

        // 点赞按钮点击事件
        likeButton.setOnClickListener(v -> toggleLike());

        // 转发按钮点击事件
        shareButton.setOnClickListener(v -> shareVideo());

        // 收藏按钮点击事件
        favoriteButton.setOnClickListener(v -> toggleFavorite());

        // 评论按钮点击事件
        commentButton.setOnClickListener(v -> openComments());
    }


    // 播放视频
    private void playVideo(String videoUrl) {
        MediaItem mediaItem = MediaItem.fromUri("asset:///videos/" + videoUrl);
        MediaSource mediaSource = new ProgressiveMediaSource.Factory(new DefaultDataSourceFactory(this, "ExoPlayer"))
                .createMediaSource(mediaItem);
        exoPlayer.setMediaSource(mediaSource);
        exoPlayer.prepare();
        exoPlayer.play();
        isPlaying = true;
        playPauseButton.setImageResource(R.drawable.more_img); // 切换为暂停按钮
    }

    // 切换播放和暂停状态
    private void togglePlayPause() {
        if (isPlaying) {
            exoPlayer.pause();
            isPlaying = false;
            playPauseButton.setImageResource(R.drawable.more_img); // 切换为播放按钮
        } else {
            exoPlayer.play();
            isPlaying = true;
            playPauseButton.setImageResource(R.drawable.more_img);
        }
    }

    // 切换全屏
    private void toggleFullScreen() {
        // 实现全屏切换逻辑
    }

    // 点赞按钮点击事件
    private void toggleLike() {
        isLiked = !isLiked;
        likeButton.setImageResource(isLiked ? R.drawable.ic_star_filled : R.drawable.ic_star_outline);
    }

    // 转发按钮点击事件
    private void shareVideo() {
        // 实现分享视频逻辑
    }

    // 收藏按钮点击事件
    private void toggleFavorite() {
        isFavorited = !isFavorited;
        favoriteButton.setImageResource(isFavorited ? R.drawable.ic_star_filled : R.drawable.ic_star_outline);
    }

    // 评论按钮点击事件
    private void openComments() {
        // 实现打开评论页面逻辑
    }

    // 进度条点击事件
    private void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        // 实现进度条点击事件逻辑
    }

    // 进度条开始拖动事件
    private void onStartTrackingTouch(SeekBar seekBar) {
        // 实现进度条开始拖动事件逻辑
    }

    // 进度条停止拖动事件
    private void onStopTrackingTouch(SeekBar seekBar) {
        // 实现进度条停止拖动事件逻辑
    }
}